<template>
  <header class="header">
    <div class="wrap content">
      <RouterLink to="/">
        <i class="iconfont icon-taiyang1"></i>
        <h1>新中地天气</h1>
      </RouterLink>
      <div class="weather-local">
        <h4>{{ localWeather.city }}</h4>
        <span>
          实时天气: {{ localWeather.weather }} {{ localWeather.temperature }}℃
          {{ localWeather.winddirection }}风{{ localWeather.windpower }}级
        </span>
      </div>
      <div class="info">
        <i class="iconfont icon-tishi2" @click="TipsShow = true"></i>
        <i class="iconfont icon-tianjia" @click="handleAddCityWeather" v-if="iconShow"></i>
      </div>
    </div>
  </header>
  <Tips :TipsShow="TipsShow" @handleClose="TipsShow = false" />
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { getCityByIP, getBaseWeatherByAdcode } from '@/api/index.js';
import { useRoute } from 'vue-router';
import { WeatherStore } from '@/stores/counter.js';
import { storeToRefs } from 'pinia';
const weatherStore = WeatherStore();
const { addCityWeather } = weatherStore;
const { cityWeatherList } = storeToRefs(weatherStore);
import Tips from './Tips.vue';

const localWeather = ref({});
const searchCityWeather = ref({});
const TipsShow = ref(false);
const route = useRoute();

const iconShow = computed(() => {
  return (
    route.name === 'cityweather' &&
    !cityWeatherList.value.find((item) => item.city === route.query.search)
  );
});

// 添加城市天气信息
const handleAddCityWeather = () => {
  addCityWeather(searchCityWeather.value);
};

// 获取当前城市实时天气
onMounted(async () => {
  const res = await getCityByIP();
  const res2 = await getBaseWeatherByAdcode(res.adcode);
  localWeather.value = res2.lives[0];
});

// 监听路由变化，获取城市天气信息
watch(
  route,
  async () => {
    if (route.name === 'cityweather') {
      const res = await getBaseWeatherByAdcode(route.params.Adcode);
      searchCityWeather.value = res.lives[0];
    }
  },
  { immediate: true }
);
</script>

<style lang="scss" scoped>
@import url('//at.alicdn.com/t/c/font_4870236_ivz6gw60sbo.css');
.header {
  //sticky 粘性定位:让header固定在顶部
  position: sticky;
  top: -1px;
  width: 100%;
  height: 80px;
  color: #fff;
  background-color: #00668a;
  box-shadow: 0 3px 4px #304b5f;
  z-index: 999;
  h1 {
    font-size: 1.5rem;
    line-height: 2rem;
  }
  .content {
    height: 100%;
    display: flex;
    gap: 20px;
    justify-content: space-between;
    align-items: center;
    margin: 0 auto;
    .router-link-active {
      display: flex;
      gap: 10px;
      color: #fff;
      font-size: 24px;
      text-decoration: none;
      align-items: center;
      i {
        font-size: 32px;
      }
    }
    .weather-local {
      flex: 1;
      display: flex;
      gap: 1rem;
      align-items: center;
      h4 {
        font-weight: normal;
        font-size: 20px;
        line-height: 20px;
      }
      span {
        font-size: 16px;
        line-height: 16px;
      }
    }
    .info {
     /*  img {
        margin-left: 10px;
        cursor: pointer;
        width: 20px;
        height: 20px;
      } */
      i {
        /* 鼠标悬停时显示为小手型 */
        cursor: pointer;
        font-size: 24px;
        margin-left: 5px;
        /* 鼠标悬停时颜色变化 */
        &:hover {
          color:#02445c;
          transition: color 0.8s ease;
        }
      }
    }
  }
}
</style>